package cn.com.oceansoft.business.mobile.danger.controller;

import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.mobile.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.mobile.danger.entity.DangerContent;
import cn.com.oceansoft.business.mobile.danger.entity.Dict;
import cn.com.oceansoft.business.mobile.danger.service.ICollecterService;
import cn.com.oceansoft.business.mobile.danger.service.IDnagerContentService;
import cn.com.oceansoft.business.mobile.danger.util.PicThread;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2018/4/27.
 */
@Controller
@RequestMapping("api/danger/content")
public class ApiDangerContentController {
    @Resource
    private IDnagerContentService dnagerContentService;
    @Resource
    private ICollecterService collecterService;

    @RequestMapping(value = "gotopageWithUser/{pageName}")
    public String gotoMattersPageWithUser(@PathVariable("pageName") String pageName, HttpServletRequest request, Model model) throws IllegalArgumentException, IOException {
       /* String   openId=request.getParameter("openId")  ;
        request.getSession().setAttribute("openId",openId);*/
        CollecterInfo collecterInfo = collecterService.findByOpenid((String) request.getSession().getAttribute("openId"));
            if(collecterInfo!=null){
                Dict dict=new Dict();
                dict.setDbid(-1);
                List<Dict> list=dnagerContentService.getDict(dict);
                JSONArray array=new JSONArray();
                for(Dict d:list){
                    JSONObject obj=new JSONObject();
                    obj.put("value",d.getName());
                    obj.put("code",d.getCode());
                    obj.put("id",d.getDbid());
                    array.add(obj);
                    model.addAttribute("dicttype"+d.getCode(),dnagerContentService.getDict(d));
                }
                model.addAttribute("type",array);
                model.addAttribute("collecterInfo",collecterInfo);
                return "mobile/danger/" + pageName;
            }else{
                String url="http://wjw.szgaj.cn/aqyh/api/collecter/collecterInfo";
                return "redirect:"+url;
            }

       // }
    }
    @RequestMapping(value = "gotopage/{pageName}")
    public String gotopage(@PathVariable("pageName") String pageName, HttpServletRequest request, Model model) throws IllegalArgumentException, IOException {
//ol7bHtxpijbSUxxJSW_DkUj6vht8
        //request.getSession().setAttribute("openId","ol7bHtxpijbSUxxJSW_DkUj6vht8");
                return "mobile/danger/" + pageName;
    }

    /**
     * 获取街路巷
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getJlx")
    public Result getSsq(HttpServletRequest request) {
        String name="";
        name=request.getParameter("name");
       // JSONArray list= DictUtil.getJlxListByName(name);
       /* System.out.println("=========JSONArray========");
        System.out.println(list);*/
        JSONArray list=dnagerContentService.getJlxListByName(name);
        Result result = new Result();
        result.setData(list);
        return  result;
    }

    /**
     * 隐患信息申报
     * @param request
     * @param dangerContent
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Result submit(HttpServletRequest request, DangerContent dangerContent) {
       // Member member = (Member) request.getSession().getAttribute(Constant.SESSION_USER);
        dangerContent.setOpenid((String) request.getSession().getAttribute("openId"));
        dangerContent.setCreatetime(new Date());
        dangerContent.setSource("1");
        SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd");
        String date2 = format.format(new Date());//格式化数据
        dangerContent.setPhoto2(date2);
        List<DangerContent> list=new ArrayList<DangerContent>();
        list= dnagerContentService.getListByTime(dangerContent);
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss");
        String date = formatter.format(new Date());//格式化数据
        if(list==null){
            dangerContent.setSeqid("BJ"+date+"00"+1);
        }else{
            dangerContent.setSeqid("BJ"+date+"00"+(list.size()+1));
        }

        //dangerContent.setId(UUID.randomUUID().toString());
        dangerContent.setStatus("2");//上传图片来源上报
        Result result=new Result();
        try{
        dnagerContentService.saveDangerContent(dangerContent);
         result.setData(dangerContent.getSeqid());
         result.setSucc(true);
         result.setMsg("申报成功");
          PicThread thread=new PicThread(dangerContent);
        thread.start();
         return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setSucc(false);
        result.setMsg("申报失败");
        return result;
    }
}
